﻿using UnityEngine;
using System.Collections;

public enum ETurnPlayerSelect
{
	None = 0,
	player_1,
	player_2,
	player_3,
	player_4,
	enemy_1 = 5,
	enemy_2,
	enemy_3,
	enemy_4,
}

[System.Serializable]
public class InfiniteTurn
{
	public ETurnPlayerSelect attacker;
	public ETurnPlayerSelect defender;
}

public class BattleCheat : MonoBehaviour {

	private static BattleCheat instance;
	
	public static BattleCheat Instance{
		get{
			return instance;
		}
	}
	
	void Awake()
	{
		instance = this;
	}

	public bool noSupportAttack;
	public bool alwaysDual;
	public bool alwaysTriple;
	public bool alwaysBurst;
	public bool alwaysSupportDefense;
	public ETurnPlayerSelect supportDefenser;
	public bool noSupportDef;
	public bool alwaysSkill;
	public bool noSkill;
	public InfiniteTurn infiniteTurn;
	public float timeScale=1.0f;
	float timeScaleOld;
	public bool useCameraIdle = true;
	bool useCameraIdleOld;
	public bool rageSkill;
	//public int damageCheat= -1;
	public int damageCheatPlayer = -1;
	public int damageCheatEnemy = -1;
	public float deltaRage = -1;
	public ETurnPlayerSelect revive;
	ETurnPlayerSelect reviveOld;
	public bool gameWin;
	public bool gameLose;
	public bool forceNextWave;
	public bool cheatModel;
	public int [] playerModelId;
	public int [] enemyModelId;
	public bool cheatStartCharacter;
	public int [] enemyStartId;
	//public bool nextTurnBurstAll;
	public bool activateOverdrive;
	public bool randomAttackOrder;
	public string [] enemyHPBarName;
	public string playerHPBarName;
	public bool turnDoesntStart;
	public bool stopIdleCam;
	public bool timeOverDontEnd;

	void Start()
	{
		reviveOld = revive;
		useCameraIdleOld = useCameraIdle;
	}

	public bool IsFixedSupportDefenser(BattlePawn pawn)
	{
		if(supportDefenser== ETurnPlayerSelect.None)
			return false;
		if( (int)supportDefenser <=4 && pawn.isPlayer && pawn.mNo == (int)supportDefenser)
			return true;
		if( (int)supportDefenser >=5 && !pawn.isPlayer && pawn.mNo == (int)supportDefenser-4)
			return true;
		return false;
	}

	public bool CanSupportAttack(int nCount)
	{
		if(alwaysDual && nCount==0)
			return true;
		if(alwaysTriple && nCount<=1)
			return true;
		if(alwaysBurst && nCount<=2)
			return true;
		return false;
	}

	public bool IsAlwaysSupportAtk()
	{
		if(alwaysDual || alwaysTriple || alwaysBurst)
			return true;
		return false;
	}

	public bool CheckInfiniteTurnAttacker(BattlePawn pawn)
	{
//		if(infiniteTurn!=null && infiniteTurn.parentPawn != pawn)
//			return true;
		if(infiniteTurn.attacker == ETurnPlayerSelect.None)
			return false;
		if(pawn.isPlayer) {
			if(infiniteTurn.attacker == ETurnPlayerSelect.player_1 && pawn.mNo == 1)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.player_2 && pawn.mNo == 2)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.player_3 && pawn.mNo == 3)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.player_4 && pawn.mNo == 4)
				return false;
		}
		else {
			if(infiniteTurn.attacker == ETurnPlayerSelect.enemy_1 && pawn.mNo == 1)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.enemy_2 && pawn.mNo == 2)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.enemy_3 && pawn.mNo == 3)
				return false;
			if(infiniteTurn.attacker == ETurnPlayerSelect.enemy_4 && pawn.mNo == 4)
				return false;
		}
		return true;
	}

	public BattlePawn CheckInfiniteTurnDefender(bool isPlayer)
	{
		if(infiniteTurn.defender == ETurnPlayerSelect.None)
			return null;
		if(isPlayer) {
			if(infiniteTurn.defender == ETurnPlayerSelect.enemy_1)
				return BattleManager.Instance.GetEnemyTargetable(1);
			if(infiniteTurn.defender == ETurnPlayerSelect.enemy_2)
				return BattleManager.Instance.GetEnemyTargetable(2);
			if(infiniteTurn.defender == ETurnPlayerSelect.enemy_3)
				return BattleManager.Instance.GetEnemyTargetable(3);
			if(infiniteTurn.defender == ETurnPlayerSelect.enemy_4)
				return BattleManager.Instance.GetEnemyTargetable(4);
		}
		else {
			if(infiniteTurn.defender == ETurnPlayerSelect.player_1)
				return BattleManager.Instance.GetPlayerTargetable(1);
			if(infiniteTurn.defender == ETurnPlayerSelect.player_2)
				return BattleManager.Instance.GetPlayerTargetable(2);
			if(infiniteTurn.defender == ETurnPlayerSelect.player_3)
				return BattleManager.Instance.GetPlayerTargetable(3);
			if(infiniteTurn.defender == ETurnPlayerSelect.player_4)
				return BattleManager.Instance.GetPlayerTargetable(4);
		}
		return null;
	}

	void Update()
	{
		if(timeScale != timeScaleOld)
		{
			timeScaleOld = timeScale;
			Time.timeScale = timeScale;
		}
		if(useCameraIdle != useCameraIdleOld)
		{
			BattleManager.Instance.TurnIdleCamera(useCameraIdle);
			useCameraIdleOld = useCameraIdle;
		}
		if(revive!= reviveOld)
		{
			reviveOld = revive;
			int reviveNo = (int)revive;
			if(reviveNo>=1 && reviveNo<=4)
				BattleManager.Instance.RevivePlayer(reviveNo);
			else if(reviveNo>4) {
				reviveNo = reviveNo-4;
				BattleManager.Instance.ReviveEnemyPlayer(reviveNo);
			}
		}

		if(rageSkill) {
			rageSkill = false;
			BattleManager.Instance.PrepareRageSkill();
		}

		//if(nextTurnBurstAll) {
		//	nextTurnBurstAll = false;
		//	BattleManager.Instance.SetNextTurnBurstAll();
		//}

		if(activateOverdrive) {
			activateOverdrive = false;
			BattleManager.Instance.SetOverdriveMode();
		}

	}
}
